package per.hx.bs.web.sys.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import per.hx.bs.base.BaseController;
import per.hx.bs.base.annotation.BsCLog;
import per.hx.bs.base.domain.ReturnDTO;
import per.hx.bs.entity.sys.SysDictDO;
import per.hx.bs.entity.sys.SysDictTypeDO;
import per.hx.bs.util.PageUtils;
import per.hx.bs.util.Query;
import per.hx.bs.web.sys.service.SysDictService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author HuXing
 * @date 2018/3/27  17:06
 */
@Controller
@RequestMapping("sys/dict")
public class DictController extends BaseController {

    private final String prefix = "system/dict/";


    private final SysDictService sysDictService;

    @Autowired
    public DictController(SysDictService sysDictService) {
        this.sysDictService = sysDictService;
    }


    @BsCLog("进入数据字典")
    @RequiresPermissions("sys:dict:dict")
    @GetMapping(value = {"/", ""})
    public String dict() {
        return prefix + "dict";
    }

    @BsCLog("获取数据字典数据")
    @RequiresPermissions("sys:dict:dict")
    @GetMapping("/list")
    @ResponseBody
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        Query query = new Query(params);
        List<SysDictDO> dictList = sysDictService.list(query);
        int total = sysDictService.count(query);
        return new PageUtils(dictList, total);
    }

    @BsCLog("进入数据字典添加")
    @RequiresPermissions("sys:dict:add")
    @GetMapping("/add")
    public String add() {
        return prefix + "add";
    }

    @BsCLog("进入数据字典修改界面")
    @GetMapping("/edit/{id}")
    @RequiresPermissions("sys:dict:edit")
    public String edit(@PathVariable("id") Long id, Model model) {
        SysDictDO dict = sysDictService.get(id);
        model.addAttribute("dict", dict);
        return prefix + "edit";
    }

    @BsCLog("保存数据字典")
    @RequiresPermissions("sys:dict:add")
    @PostMapping(value = "/save")
    @ResponseBody
    public ReturnDTO save(SysDictDO sysDictDO) {
        return ReturnDTO.check(sysDictService.save(sysDictDO));
    }

    @BsCLog("更新数据字典")
    @RequiresPermissions("sys:dict:edit")
    @PostMapping(value = "/update")
    @ResponseBody
    public ReturnDTO update(SysDictDO sysDictDO) {
        sysDictDO.setUpdateId(getUserId());
        return ReturnDTO.check(sysDictService.update(sysDictDO));
    }

    @BsCLog("删除数据字典")
    @RequiresPermissions("sys:dict:remove")
    @PostMapping(value = "/remove")
    @ResponseBody
    public ReturnDTO remove(Long id) {
        return ReturnDTO.check(sysDictService.remove(id));
    }

    @BsCLog("批量删除数据字典")
    @RequiresPermissions("sys:dict:remove")
    @PostMapping(value = "/batchRemove")
    @ResponseBody
    public ReturnDTO batchRemove(@RequestParam("ids[]") Long[] ids) {
        return ReturnDTO.check(sysDictService.batchRemove(ids));
    }

    @BsCLog("进入字典数据添加界面")
    @RequiresPermissions("sys:dict:add")
    @GetMapping(value = "/add/{type}/{typeName}")
    public String addTable(Model model, @PathVariable("type") String type, @PathVariable("typeName") String typeName) {
        model.addAttribute("type", type);
        model.addAttribute("typeName", typeName);
        return prefix + "addItem";
    }

    @BsCLog("按类型获取数据字典")
    @RequiresPermissions("sys:dict:dict")
    @GetMapping(value = "/list/{type}")
    @ResponseBody
    public List<SysDictDO> listByType(@PathVariable("type") String type) {
        Map<String, Object> map = new HashMap<String, Object>(1);
        map.put("type", type);
        return sysDictService.list(map);
    }

    @BsCLog("进入数据字典类型管理界面")
    @RequiresPermissions("sys:dict:dict")
    @GetMapping(value = "/typeMange")
    public String typeMange() {
        return prefix + "typeMange";
    }

    @BsCLog("获取数据字典类型")
    @RequiresPermissions("sys:dict:dict")
    @GetMapping("/type/listByMap")
    @ResponseBody
    public PageUtils listTypeByMap(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        Query query = new Query(params);
        List<SysDictTypeDO> dictList = sysDictService.listType(query);
        int total = sysDictService.countType(query);
        return new PageUtils(dictList, total);
    }


    @BsCLog("获取数据字典类型")
    @RequiresPermissions("sys:dict:dict")
    @GetMapping(value = "/type")
    @ResponseBody
    public List<SysDictTypeDO> listType() {
        return sysDictService.listType(new HashMap<String, Object>(0));
    }

    @BsCLog("添加数据字典类型")
    @RequiresPermissions("sys:dict:add")
    @GetMapping(value = "/type/add")
    public String saveType() {
        return prefix + "typeAdd";
    }

    @BsCLog("检查数据是否存在")
    @RequiresPermissions("sys:dict:dict")
    @PostMapping(value = "/type/exit")
    @ResponseBody
    public boolean saveType(@RequestParam Map<String, Object> params) {
        return !sysDictService.typeExit(params);
    }

    @BsCLog("添加数据字典类型")
    @RequiresPermissions("sys:dict:add")
    @PostMapping(value = "/type/save")
    @ResponseBody
    public ReturnDTO saveType(SysDictTypeDO dictTypeDO) {
        dictTypeDO.setCreateId(getUserId());
        return ReturnDTO.check(sysDictService.save(dictTypeDO));
    }

    @BsCLog("添加数据字典类型")
    @RequiresPermissions("sys:dict:edit")
    @GetMapping(value = "/type/edit/{id}")
    public String editType(@PathVariable("id") Long id, Model model) {
        model.addAttribute("type", sysDictService.type(id));
        return prefix + "typeEdit";
    }

    @BsCLog("更新数据字典类型")
    @RequiresPermissions("sys:dict:edit")
    @PostMapping(value = "/type/edit")
    @ResponseBody
    public ReturnDTO updateType(SysDictTypeDO dictTypeDO) {
        dictTypeDO.setUpdateId(getUserId());
        return ReturnDTO.check(sysDictService.updateType(dictTypeDO));
    }

    @BsCLog("删除数据字典类型")
    @RequiresPermissions("sys:dict:remove")
    @GetMapping(value = "/type/remove")
    @ResponseBody
    public ReturnDTO updateType(@RequestParam("id") Long id, @RequestParam("type") String type) {
        Map<String, Object> map = new HashMap<String, Object>(1);
        map.put("type", type);
        if (sysDictService.count(map) > 0) {
            return ReturnDTO.error("其下有数据信息,无法删除！！！");
        }
        return ReturnDTO.check(sysDictService.removeType(id));
    }
}
